package com.base.sms.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.enums.SmsReportStatus;
import com.base.common.enums.TerminalType;
import com.base.common.json.JsonUtil;
import com.base.sms.service.SmsService;
import com.cjy.common.bean.OpAttributes;

/**
 * 邮箱消息action
 * 
 * @author Administrator
 * 
 */

@Action("smsAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class SmsAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private SmsService smsService;

	// 消息查询参数
	private int bid;
	private int page; // 当前页数
	private String smsReportIds; // report id 集合，以逗号分隔
	private int smsId; // 消息id
	private String code; // 手机设备码
	private int categories; // 消息类别

	// 个人消息发送和回复参数
	private String mess; // 消息内容
	private String employeeIdList; // 员工id集合json字符串

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public String getEmployeeIdList() {
		return employeeIdList;
	}

	public void setEmployeeIdList(String employeeIdList) {
		this.employeeIdList = employeeIdList;
	}

	public int getCategories() {
		return categories;
	}

	public void setCategories(int categories) {
		this.categories = categories;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getSmsReportIds() {
		return smsReportIds;
	}

	public void setSmsReportIds(String smsReportIds) {
		this.smsReportIds = smsReportIds;
	}

	public int getSmsId() {
		return smsId;
	}

	public void setSmsId(int smsId) {
		this.smsId = smsId;
	}

	public String getMess() {
		return mess;
	}

	public void setMess(String mess) {
		this.mess = mess;
	}

	/**
	 * 条件获取消息
	 * 
	 * @return
	 */
	public String getSmsByFuzzyCondition() {
		try {
			if (bid == TerminalType.CJY_ANDROID.getValue()
					|| bid == TerminalType.CJY_IOS.getValue()
					|| bid == TerminalType.CJY_WEIXIN.getValue()) {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>()
								.add("code", 1).add(
										"result",
										smsService.getSmsByFuzzyCondition(0,
												categories, code,
												SmsReportStatus.ALL.getValue(),
												page)));
			} else if (bid == TerminalType.YGJ_ANDROID.getValue()
					|| bid == TerminalType.YGJ_IOS.getValue()) {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>()
								.add("code", 1).add(
										"result",
										smsService.getSmsByFuzzyCondition(1,
												categories, code,
												SmsReportStatus.ALL.getValue(),
												page)));
			} else {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>(
								"code", 0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 条件获取消息条数
	 * 
	 * @return
	 */
	public String getSmsCountsByFuzzyCondition() {
		try {
			if (bid == TerminalType.CJY_ANDROID.getValue()
					|| bid == TerminalType.CJY_IOS.getValue()
					|| bid == TerminalType.CJY_WEIXIN.getValue()) {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>()
								.add("code", 1)
								.add("result",
										new OpAttributes<String, Integer>().add(
												"totalCounts",
												smsService
														.getSmsCountsByFuzzyCondition(
																0,
																categories,
																code,
																SmsReportStatus.ALL
																		.getValue()))));
			} else if (bid == TerminalType.YGJ_ANDROID.getValue()
					|| bid == TerminalType.YGJ_IOS.getValue()) {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>()
								.add("code", 1)
								.add("result",
										new OpAttributes<String, Integer>().add(
												"totalCounts",
												smsService
														.getSmsCountsByFuzzyCondition(
																1,
																categories,
																code,
																SmsReportStatus.ALL
																		.getValue()))));
			} else {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>(
								"code", 0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 判断是否有未读信息
	 * 
	 * @return
	 */
	public String isHaveUnreadSms() {
		try {
			if (bid == TerminalType.CJY_ANDROID.getValue()
					|| bid == TerminalType.CJY_IOS.getValue()
					|| bid == TerminalType.CJY_WEIXIN.getValue()) {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>()
								.add("code", smsService
										.getSmsCountsByFuzzyCondition(0, -1,
												code, SmsReportStatus.NOTREAD
														.getValue()) > 0 ? 1
										: 0));
			} else if (bid == TerminalType.YGJ_ANDROID.getValue()
					|| bid == TerminalType.YGJ_IOS.getValue()) {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>()
								.add("code", smsService
										.getSmsCountsByFuzzyCondition(1, -1,
												code, SmsReportStatus.NOTREAD
														.getValue()) > 0 ? 1
										: 0));
			} else {
				actionResult = ActionResult
						.getActionResult(new OpAttributes<String, Integer>(
								"code", 0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 更新消息记录状态
	 * 
	 * @return
	 */
	public String updateSmsToReadReportStatus() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>().add(
							"code",
							smsService
									.updateReadTimeStatusByReportId(smsReportIds
											.split(","))));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 给其他人发送信息
	 * 
	 * @return
	 */
	public String sendSmsToOther() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							smsService.sendSmsToOther(JsonUtil.string2Obejct(
									employeeIdList, List.class), mess)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 消息回复
	 * 
	 * @return
	 */
	public String replySms() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							smsService.replySms(JsonUtil.string2Obejct(
									employeeIdList, List.class), smsId, mess)));

		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 删除短消息
	 * 
	 * @return
	 */
	public String deleteSms() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							smsService.deleteSms(smsReportIds)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	/**
	 * 删除个人消息
	 * 
	 * @return
	 */
	/*
	 * public String deletePersonSms(){ try { actionResult =
	 * ActionResult.getActionResult(new OpAttributes<String, Integer>( "code",
	 * smsService.deletePersonSms(smsReportIds))); } catch (Exception e) {
	 * e.printStackTrace(); rn returnType(); } actionResult =
	 * ActionResult.getActionResult(new OpAttributes<String, Integer>( "code",
	 * 0)); } retu
	 */
}
